package com.thread.www.demo;

import com.google.common.collect.Maps;
import com.thread.www.ThreadLockTest;

import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.Executors;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

/**
 * Created by 27340 on 2017/10/27.
 */
public class MyLockDemo {
    public final static ReentrantLock lock = new ReentrantLock();
    public final Condition condition1 = lock.newCondition();
    public final Condition condition2 = lock.newCondition();
    static MyLockDemo demo = new MyLockDemo();



    public void awaitstep1() throws InterruptedException {
        lock.lock();
        System.out.println(Thread.currentThread().getName() + "lock step1");
        Thread.sleep(1000);
        condition1.await();
        System.out.println(Thread.currentThread().getName() + "step1 end");
    }

    public void step1() throws InterruptedException {
        lock.lock();
        System.out.println(Thread.currentThread().getName() + "lock step1");
        Thread.sleep(1000);
    }

    public void trystep1() throws InterruptedException {
        if(lock.tryLock()){
            System.out.println(Thread.currentThread().getName() + "lock step1");
            Thread.sleep(1000);
            condition1.await();
        }
    }

    public void awaitstep2() throws InterruptedException {
        lock.lock();
        System.out.println(Thread.currentThread().getName() + "lock step2");
        Thread.sleep(1000);
        condition2.await();
    }

    public void step2() throws InterruptedException {
        lock.lock();
        System.out.println(Thread.currentThread().getName() + "lock step2");
        Thread.sleep(1000);
    }

    public void trystep2() throws InterruptedException {
        if(lock.tryLock()){
            System.out.println(Thread.currentThread().getName() + "lock step2");
            Thread.sleep(1000);
            condition2.await();
        }
    }

    public void step3() throws InterruptedException {
        lock.lock();
        System.out.println(Thread.currentThread().getName() + "lock step3");
        try {
            condition1.signal();
        }finally {
            lock.unlock();
        }
    }


    public void notify1(){
        if(lock.tryLock()){
            System.out.println(Thread.currentThread().getName() + "lock step3");
            try {
                System.out.println("start notify1");
                condition1.notify();
            }finally {
                lock.unlock();
            }
        }
    }



    public static void main(String[] args) throws InterruptedException {
        Executors.newFixedThreadPool(10);
        Executors.newCachedThreadPool();
        Executors.newSingleThreadExecutor();
        ConcurrentHashMap map = new ConcurrentHashMap();
        Thread t1 = new Thread(){
            @Override public void run(){
                try {
                    demo.awaitstep1();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        };

        Thread t2 = new Thread(){
            @Override public void run(){
                try {
                    demo.awaitstep2();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        };

        Thread t3= new Thread(){
            @Override public void run(){
                try {
                    demo.step3();
                    Thread.sleep(1000L);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        };
        t1.start();
        t2.start();
        Thread.sleep(1000L);
        t3.start();
    }


}
